// import {configurationenvironment} from '../util/environment.js';

const request = (url, method, data, isForm = true) => { 
	//let ServiceEnvironment = uni.getStorageSync('ServiceEnvironment'); //当前环境状态
	//let urlConfig = configurationenvironment(ServiceEnvironment);
	let urlConfig = 'lhz.rigourtech.com:8091';
	let finalData = {};
	let body = data || {};
	let token = uni.getStorageSync('token');
	let userId = uni.getStorageSync('userId');
	
	let common = {
		// token: token || '',
		// userId
	}
	
	finalData = Object.assign(common, body);
	
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${urlConfig}${url}`,
			method,
			data: finalData,
			header: {
			    'content-type': isForm ? 'application/x-www-form-urlencoded' : 'application/json',
		    },
		}).then(res => {
			const loactionUrl = [  //测试打印全部传参的接口
			
			];
			let us = loactionUrl.includes(url);
			if(us) {
				console.log('finalRes', res);
				console.log('finalData', finalData);
			}
			
			if (res[1].data.code && res[1].data.code == 200) {
				return resolve(res[1].data);
			} else {
				const urls = [  //不需要展示错误提示的接口
				
				];
				
				let urlStatus = urls.includes(url);
				
				if(!urlStatus && res[1].data.message) {
					uni.showToast({
			　　　　　　 title: res[1].data.message,
			            icon: 'none',
						duration: 2000
		　　　　　　  });
				}
				if(res[1].data.code == -3){ //token过期 无权限操作
					uni.setStorageSync('token', '');
					uni.setStorageSync('userId', '');
					console.log('requestToken....', uni.getStorageSync('token'));
				    setTimeout(()=>{ //跳转登陆
						uni.reLaunch({
							
						})
					},500)
				}
	            return reject(res[1].data) ;
			}
		}).catch(parmas =>{
			console.log('服务端异常',parmas);
			uni.showToast({
	　　　　　　 title: '请求超时，请重试',
	            icon: 'none',
　　　　　　  });
		    return reject(parmas);
		})
	})
}

export default request;